package com.tsvetanv.data.binding.nested.collections.array;

import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.web.bind.ServletRequestDataBinder;

import com.tsvetanv.data.binding.model.nested.NestedArrayCommandBean;

import junit.framework.TestCase;

public class NestedArrayBindTest extends TestCase {
	private NestedArrayCommandBean bean;
	private ServletRequestDataBinder binder;
	private MockHttpServletRequest request;

	@Override
	protected void setUp() throws Exception {
		bean = new NestedArrayCommandBean();
		binder = new ServletRequestDataBinder(bean, "beanName");
		request = new MockHttpServletRequest();
	}

	public void testNestedArrayBind() {
		request.addParameter("names[2].firstName", "Tsvetan");
		request.addParameter("names[1].lastName", "Vasilev");

		binder.bind(request);

		assertEquals("Tsvetan", bean.getNames()[2].getFirstName());
		assertEquals("Vasilev", bean.getNames()[1].getLastName());

		assertNotNull(bean.getNames()[0]);
		assertNull(bean.getNames()[2].getLastName());
		assertNull(bean.getNames()[1].getFirstName());
	}

}
